/* api */
import * as ProjectManageApi from '@src/api/ProjectManage';
import * as PassApi from '@src/api/PassApi';
/* entity */
import Field from '@model/entity/Field';
/* model */
import { systemFields } from '@src/modules/projectManage/taskManageList/model';
import MsgModel from '@model/MsgModel';
import Page from '@model/Page';
/* enum */
import TableNameEnum from '@model/enum/TableNameEnum';
/* util */
import { message } from '@src/util/message';
import Log from '@src/util/log';
import Platform from '@src/util/platform';
import platform from '@src/platform';
import i18n from '@src/locales';
/* hooks */
import { useLoading } from '@hooks/useLoading';
/* vue */
import { Ref, ref } from 'vue';

type reloadTabType = () => void;

// 获取任务详情数据
function useFetchTaskViewData() {
    const detailData = ref({});

    const fetchTaskViewData = (searchParams: any) => {

        return (
            ProjectManageApi.taskDetailPro(searchParams).then((res: any) => {
                if (MsgModel.isSuccess(res)) {
                    detailData.value = res?.data || {}
                } else {
                    message.error(res.message);
                }

                return detailData.value;
            }).catch((error: any) => {
                Log.error(error, fetchTaskViewData.name);
            })
        )
    }

    return {
        detailData,
        fetchTaskViewData,
    }
}

// 获取通用任务详情数据
function useFetchCommonTaskData() {
    // 加载状态
    const { showLoading, hideLoading } = useLoading();

    const commonTaskData = ref({});

    const fetchCommonTaskData = (searchParams: any) => {
        showLoading();

        return (
            ProjectManageApi.comTaskDetailPro(searchParams).then((res: any) => {
                if (MsgModel.isSuccess(res)) {
                    commonTaskData.value = res?.data || {}
                } else {
                    message.error(res.message);
                }

                return commonTaskData.value;
            }).catch((error: any) => {
                Log.error(error, fetchCommonTaskData.name);
            }).finally(() => {
                hideLoading();
            })
        )
    }

    return {
        commonTaskData,
        fetchCommonTaskData,
    }
}

// 删除数据
function useFetchTaskManageDeleteData() {
    // 刷新tab
    let reloadTab: reloadTabType | null = () => {
        // @ts-ignore
        let id = window.frameElement?.dataset?.id;
        let fromId = window.frameElement?.getAttribute('fromid');

        // 打开相应的tab 关闭当前tab
        platform.refreshTab(fromId);
        platform.closeTab(id);
        platform.goOpenTab(fromId);
    };

    const fetchTaskManageDeleteData = (params: any) => {
        return (
            ProjectManageApi.taskDeletePro(params).then((res: any) => {
                if (!res.success) {
                    reloadTab = null;
                    return Platform.toast(res.message, 'error');
                }

                Platform.toast(i18n.t('common.base.deleteSuccess') as string, 'success');
            }).catch((error: any) => {
                Log.error(error, fetchTaskManageDeleteData.name);
            })
        )
    }

    return {
        reloadTab,
        fetchTaskManageDeleteData,
    }
}

// 更新进度
function useFetchTaskManageUpdateProgress() {

    const fetchTaskManageUpdateProgress = (params: any, ref: any, func: any) => {
        return (
            ProjectManageApi.updateTaskManageProgress(params).then((res: any) => {
                if (!res.success) {
                    return Platform.toast(res.message, 'error');
                }

                Platform.toast(i18n.t('common.base.tip.updateSuccess') as string, 'success');
                ref.value.handleClose();
                func();
            }).catch((error: any) => {
                Log.error(error, fetchTaskManageUpdateProgress.name);
            })
        )
    }

    return {
        fetchTaskManageUpdateProgress,
    }
}

// 编辑
function useFetchEditTaskManageData() {
    const editTaskManageData = (params: any, actionType: string, func: any) => {
        let fetchUrl = 'updateTaskAndCommonTask';

        // 暂存状态
        if (actionType === 'draft') {
            fetchUrl = 'updateTaskAndAddCommonTask';
        }

        // @ts-ignore
        ProjectManageApi[fetchUrl](params).then((res: any) => {
            commonAction(res, func);
        })
    }

    const commonAction = (res: any, func: any) => {
        if (!res.success) return Platform.toast(res.message, 'error');
        Platform.toast(i18n.t('common.base.tip.operationSuccess') as string, 'success');
    
        func();
    }
    
    return {
        editTaskManageData
    }
}

// 取消
function useCancelTaskManageData() {
    
    const cancelTaskManageData = (params: any, fn: any) => {
        return (
            ProjectManageApi.cancelTaskManage(params).then((res: any) => {
                if (!res.success) {
                    return Platform.toast(res.message, 'error');
                }

                Platform.toast(i18n.t('common.base.tip.cancelSuccess') as string, 'success');
                fn();
            }).catch((error: any) => {
                Log.error(error, cancelTaskManageData.name);
            })
        )
    }
    
    return {
        cancelTaskManageData
    }
}

export {
    useFetchTaskViewData,
    useFetchCommonTaskData,
    useFetchTaskManageDeleteData,
    useFetchTaskManageUpdateProgress,
    useFetchEditTaskManageData,
    useCancelTaskManageData,
}
